//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.test.apps.skins.mportal3;

import com.actionsoft.apps.resource.AppContext;
import com.actionsoft.apps.skins.mportal3.cache.MPortalConfigCache;
import com.actionsoft.apps.skins.mportal3.model.MPortalConfigModel;
import com.actionsoft.apps.skins.mportal3.util.MPortalConfigFileUtils;
import com.actionsoft.apps.skins.mportal3.util.MPortalProfileNavUtil;
import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.htmlframework.AlertWindow;
import com.actionsoft.bpms.commons.htmlframework.HtmlPageTemplate;
import com.actionsoft.bpms.commons.portal.pub.web.PublicPortalWeb;
import com.actionsoft.bpms.commons.portal.skins.AbstPortalSkins;
import com.actionsoft.bpms.commons.portal.skins.PortalSkinsInterface;
import com.actionsoft.bpms.commons.security.ac.model.AccessControlModel;
import com.actionsoft.bpms.org.model.UserModel;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.fs.impl.PhotoProcessor;
import com.actionsoft.bpms.util.UtilString;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.AppAPI;
import com.actionsoft.sdk.local.api.PermAPI;
import com.actionsoft.sdk.local.api.PortalAPI;

import java.util.*;

import jodd.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class MportalSkins extends AbstPortalSkins implements PortalSkinsInterface {
    public MportalSkins() {
    }

    @Override
    public String getHomePage(UserContext me) {

        Map<String, Object> result = new HashMap();
        result.put("canChange",false);
        /**
         * 查询数据
         */
        List<BO> list1 = SDK.getBOAPI().query("BO_EU_PORTAL_NA").list();
        if (list1 != null && list1.size() > 0) {
            /**
             * 判断有无权限
             */
            for (BO bo:list1){
                boolean result11 = SDK.getPermAPI().havingACPermission(me.getUID(),"platform.process",bo.getId(),0);
                if (result11){
                    result.put("canChange",true);

                    /**
                     * 判断权限
                     */
                    String portaltype= me.getExtParam("portaltype")==null?"": me.getExtParam("portaltype").toString();
                    if (StringUtil.isEmpty(portaltype) || portaltype.equals("new")){
                        me.setExtParam("portaltype","new");
                        Map<String, Object> macroLibraries = new LinkedHashMap<String, Object>();
                        macroLibraries.put("sid", me.getSessionId());
                        macroLibraries.put("portalUrl",SDK.getPortalAPI().getPortalUrl());
                        return HtmlPageTemplate.merge("com.awspaas.user.apps.forehope.portal", "index.html", macroLibraries);
                    }

                }
            }
        }
        result.put("sid", me.getSessionId());
        AppAPI appApi = SDK.getAppAPI();
        PortalAPI portalApi = SDK.getPortalAPI();
        boolean isSystem = SDK.getPermAPI().isSystemAdministrator(me.getUID());
        result.put("isSystem", isSystem);
        UserModel userModel = me.getUserModel();
        result.put("companyName", me.getCompanyModel().getName());
        result.put("departmentName", me.getDepartmentModel().getName());
        result.put("uid", userModel.getUID());
        result.put("uniqueId", userModel.getUniqueId());
        result.put("userName", userModel.getUserName());
        result.put("userPhoto", portalApi.getUserPhoto(me, me.getUID()));
        result.put("userPhotoTmp", PhotoProcessor.getTmpPhotoUrl(me, me.getUID()));
        result.put("sysAppId", "_bpm.platform");
        String isPromptUploadPortraitStr = appApi.getProperty("com.actionsoft.apps.skins.mportal3", "isPromptUploadPortrait");
        boolean isPromptUploadPortrait = false;
        if (!UtilString.isEmptyByTrim(isPromptUploadPortraitStr)) {
            try {
                isPromptUploadPortrait = Boolean.parseBoolean(isPromptUploadPortraitStr);
            } catch (Exception var37) {
                var37.printStackTrace();
            }

            isPromptUploadPortrait = isPromptUploadPortrait && "../commons/img/photo.png".equals(result.get("userPhoto") == null ? "" : result.get("userPhoto").toString());
        }

        result.put("isPromptUploadPortrait", isPromptUploadPortrait);
        String uploadPortraitPop = appApi.getProperty("com.actionsoft.apps.skins.mportal3", "uploadPortraitPop");
        result.put("uploadPortraitPop", uploadPortraitPop);
        PublicPortalWeb publicPortalWeb = new PublicPortalWeb(me);
        JSONObject loginCheckSecurity = publicPortalWeb.loginCheckSecurity();
        result.putAll(loginCheckSecurity);
        String requiredUserInfo = portalApi.getRequiredUserInfo(me);
        String[] requiredUserInfoArray = requiredUserInfo.split("[|]");
        boolean requiredEmail = false;
        boolean requiredOfficeTel = false;
        boolean requiredMobile = false;
        String[] var17 = requiredUserInfoArray;
        int var18 = requiredUserInfoArray.length;

        int notificationMsgLoadFrequency;
        String reqUserInfo;
        for(notificationMsgLoadFrequency = 0; notificationMsgLoadFrequency < var18; ++notificationMsgLoadFrequency) {
            reqUserInfo = var17[notificationMsgLoadFrequency];
            if ("email".equals(reqUserInfo.toLowerCase())) {
                requiredEmail = true;
            } else if ("officetel".equals(reqUserInfo.toLowerCase())) {
                requiredOfficeTel = true;
            } else if ("mobile".equals(reqUserInfo.toLowerCase())) {
                requiredMobile = true;
            }
        }

        result.put("requiredEmail", requiredEmail);
        result.put("requiredOfficeTel", requiredOfficeTel);
        result.put("requiredMobile", requiredMobile);
        boolean isStartNotification = false;
        AppContext appCtx = appApi.getAppContext("com.actionsoft.apps.notification");
        if (appCtx != null) {
            isStartNotification = "ACTIVE".equals(appCtx.getRuntimeState());
        }

        result.put("isStartNotification", isStartNotification);
        notificationMsgLoadFrequency = Integer.parseInt(appApi.getProperty("com.actionsoft.apps.skins.mportal3", "notificationMsgLoadFrequency"));
        result.put("notificationMsgLoadFrequency", notificationMsgLoadFrequency);
        reqUserInfo = appApi.getProperty("com.actionsoft.apps.skins.mportal3", "notificationShowType");
        String notifyCollectShowNumStr = appApi.getProperty("com.actionsoft.apps.skins.mportal3", "notifyCollectShowNum");
        int notifyCollectShowNum = 0;
        if (!UtilString.isEmpty(notifyCollectShowNumStr)) {
            notifyCollectShowNum = Integer.parseInt(notifyCollectShowNumStr);
        }

        result.put("notificationShowType", reqUserInfo);
        result.put("notifyCollectShowNum", notifyCollectShowNum);
        String portalId = "f2a3b3da-32b0-46a8-8461-3a3c9569cb7d";
        portalId = this.getFirstVisitPortalConfig(me);
        if (UtilString.isEmpty(portalId)) {
            return AlertWindow.getAuthorityMessagePage("您访问的模板不存在或者未授权", (String)null);
        } else {
            MPortalConfigModel model = MPortalConfigCache.getConfigJsonByPortalId(me, portalId);
            JSONObject json = MPortalConfigFileUtils.modelToJSON(me, model);
            result.put("config", JSONObject.fromObject(model));
            result.put("portalName", json.opt("portalName"));
            result.put("favicon", json.opt("favicon"));
            JSONArray commonNavArr = json.optJSONArray("commonNavArr");
            JSONObject commonData = MPortalProfileNavUtil.getCommonNavArray(me, commonNavArr);
            commonNavArr = commonData.getJSONArray("array");
            result.put("commonNavArr", MPortalProfileNavUtil.getNavBySorted(commonNavArr));
            int commonSubNum = commonData.getInt("subCommonCount");
            result.put("commonSubNum", commonSubNum);
            JSONArray quitEntryList = MPortalProfileNavUtil.getQuitEntry(me);
            result.put("quitEntryList", quitEntryList);
            String mainNavJson = json.optString("mainNavJson");
            JSONObject mainNav = JSONObject.fromObject(mainNavJson);
            JSONArray mainNavArr = MPortalProfileNavUtil.getNavBySorted(mainNav.optJSONArray("mainNavArr"));
            mainNavArr = MPortalProfileNavUtil.getAllNavArray(me, mainNavArr);
            result.put("mainNavArr", mainNavArr);
            result.put("headerCode", json.opt("headerCode"));
            result.put("mainNavCode", mainNav.opt("mainNavCode"));
            result.put("bottomCode", json.opt("bottomCode"));
            boolean isStartRong = false;
            AppContext RongCtx = appApi.getAppContext("com.actionsoft.apps.rongim");
            if (RongCtx != null) {
                isStartRong = "ACTIVE".equals(RongCtx.getRuntimeState());
            }

            result.put("isStartRong", isStartRong);
            boolean isStartMobilePortal = false;
            AppContext mpCtx = appApi.getAppContext("com.actionsoft.apps.byod.helper");
            if (mpCtx != null) {
                isStartMobilePortal = "ACTIVE".equals(mpCtx.getRuntimeState());
            }

            result.put("isStartMobilePortal", isStartMobilePortal);
            return HtmlPageTemplate.merge("com.actionsoft.apps.skins.mportal3", "mportal.html", result);
        }
    }

    private String getFirstVisitPortalConfig(UserContext me) {
        PermAPI permApi = SDK.getPermAPI();
        List<AccessControlModel> portalConfigList = permApi.getACResourceList(me.getUID(), "m3_visit", 0);
        if (portalConfigList.size() > 0) {
            List<MPortalConfigModel> portalList = new ArrayList();
            Iterator var5 = portalConfigList.iterator();

            while(var5.hasNext()) {
                AccessControlModel acModel = (AccessControlModel)var5.next();
                String portalId = acModel._resourceId;
                if (!UtilString.isEmpty(portalId)) {
                    MPortalConfigModel model = MPortalConfigCache.getModel(portalId);
                    if (model != null) {
                        portalList.add(model);
                    }
                }
            }

            if (portalList != null && portalList.size() > 0) {
                Collections.sort(portalList, new Comparator<MPortalConfigModel>() {
                    @Override
                    public int compare(MPortalConfigModel o1, MPortalConfigModel o2) {
                        return o1.getOrder() - o2.getOrder();
                    }
                });
                return ((MPortalConfigModel)portalList.get(0)).getId();
            }
        }

        return null;
    }
}
